package com.xingongchang.baolebei.fragment;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.external.androidquery.callback.AjaxStatus;
import com.external.maxwin.view.XListView;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.adapter.GoodListLargeAdapter;
import com.xingongchang.baolebei.model.GoodsListModel;
import com.xingongchang.baolebei.model.ProtocolConst;
import com.xingongchang.baolebei.protocol.FILTER;
import com.xingongchang.baolebei.protocol.PAGINATED;

/**
 * @类名:RecommendFragment
 */
@SuppressLint("ValidFragment")
public class HomeRecommendFragment extends Fragment implements BusinessResponse,XListView.IXListViewListener {
	View mView = null;
	private XListView xlistView;
	String defaultTitle = "";
	int categoryid = 0;
	private GoodsListModel dataModel;
	private GoodListLargeAdapter largeListActivityAdapter;
	private boolean isSetAdapter = true;
	FILTER filter = new FILTER();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	public static HomeRecommendFragment newInstance(String title, int id) {
		HomeRecommendFragment fragment = new HomeRecommendFragment(title, id);
		return fragment;
	}

	public HomeRecommendFragment(String defaultTitle, int id) {
		this.defaultTitle = defaultTitle;
		this.categoryid = id;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.content_layout, null);
		xlistView = (XListView) mView.findViewById(R.id.goods_listview);
		xlistView.setPullLoadEnable(true);
		xlistView.setRefreshTime();
		xlistView.setXListViewListener(this, 1);
		dataModel = new GoodsListModel(getActivity());
//		filter.category_id = String.valueOf(categoryid);

		try {
			String filter_string = filter.toJson().toString();
			if (null != filter_string) {
				try {
					JSONObject filterJSONObject = new JSONObject(filter_string);
					filter = com.xingongchang.baolebei.protocol.FILTER
							.fromJson(filterJSONObject);
					filter.intro = dataModel.INTRO;

				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		dataModel.addResponseListener(this);

		largeListActivityAdapter = new GoodListLargeAdapter(
				getActivity(), dataModel.simplegoodsList);
		dataModel.fetchPreSearch(filter);

		return mView;
	}


	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

	}

	@Override
	public void onRefresh(int id) {
		// TODO Auto-generated method stub
		isSetAdapter = true;
		dataModel.fetchPreSearch(filter);
	}

	@Override
	public void onLoadMore(int id) {
		// TODO Auto-generated method stub
		isSetAdapter = false;
		dataModel.fetchPreSearchMore(filter);
	}

	public void setContent() {

		if (largeListActivityAdapter == null) {

			if (dataModel.simplegoodsList.size() == 0) {

			} else {
				largeListActivityAdapter = new GoodListLargeAdapter(getActivity(),
						dataModel.simplegoodsList);
				xlistView.setAdapter(largeListActivityAdapter);
			}

		} else {

			if (isSetAdapter == true) {
					xlistView.setAdapter(largeListActivityAdapter);
			} else {
				largeListActivityAdapter.dataList = dataModel.simplegoodsList;
				largeListActivityAdapter.notifyDataSetChanged();
			}
		}

	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		// TODO Auto-generated method stub
		if (url.endsWith(ProtocolConst.SEARCH)) {
			xlistView.stopRefresh();
			xlistView.stopLoadMore();
			xlistView.setRefreshTime();

			setContent();
			PAGINATED paginated = PAGINATED.fromJson(jo
					.optJSONObject("paginated"));
			if (0 == paginated.more) {
				xlistView.setPullLoadEnable(false);
			} else {
				xlistView.setPullLoadEnable(true);
			}
		}
	}
}
